﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public struct PatientStats 
{
    public string BPStats;
    public string HCountStats;
    public string HRateStats;
}

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class CalculateStatsWS : System.Web.Services.WebService
{
    private PatientStats pstats;

    public CalculateStatsWS()
    {

        pstats.BPStats = "";
        pstats.HCountStats = "";
        pstats.HRateStats = "";
    }

    private void AssignValues(string Pid)
    {

        SqlConnection conn = null;
        SqlDataReader rdr = null;

        try
        {
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn2"].ToString());
            //Server=localhost;DataBase=PatientRecord;Integrated Security=SSPI");
            conn.Open();


            SqlCommand cmd = new SqlCommand("patientstats", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Pid", Pid));

            rdr = cmd.ExecuteReader();
            if (rdr.Read())
            {

                pstats.BPStats = rdr.GetString(0);
                pstats.HCountStats = rdr.GetString(1);
                pstats.HRateStats = rdr.GetString(2);

            }

        }

        finally
        {
            if (conn != null)
            {
                conn.Close();
            }
            if (rdr != null)
            {
                rdr.Close();
            }

        }


    }


    [WebMethod]
    public PatientStats GetPatientStats(string pid) {
        AssignValues(pid);
        PatientStats PatientStats = new PatientStats();

        string pbpstats = "";
        string phcountstats = "";
        string phratestats = "";

        int bpvalue = Convert.ToInt32(pstats.BPStats);
        double hcountvalue = Convert.ToDouble(pstats.HCountStats);
        int hratevalue = Convert.ToInt32(pstats.HRateStats);
        
        
        if (bpvalue < 120)
        {
            pbpstats = "Normal";
        }
        if (bpvalue >= 120 && bpvalue <=139)
        {
            pbpstats = "Prehypertension";
        }
        if (bpvalue >= 140 && bpvalue <= 159)
        {
            pbpstats = "High BP Stage 1";
        }
        if (bpvalue >= 160 && bpvalue < 180)
        {
            pbpstats = "High BP Stage 2";
        }
        if (bpvalue >= 180)
        {
            pbpstats = "Hypertensive Crisis";
        }

        PatientStats.BPStats = pbpstats;



        if (hcountvalue <= 18 && hcountvalue >= 11)
        {
            phcountstats = "Normal";
        }
        else 
        { 
            phcountstats = "Dangerous Level";
        }
              
        PatientStats.HCountStats = phcountstats;



        if (hratevalue <= 100 && hratevalue >= 50)
        {
            phratestats = "Normal";
        }
        else
        {
            phratestats = "Dangerous Level";
        }
        
        PatientStats.HRateStats = phratestats;


        return PatientStats;


    }
    
}
